package cn.rongdao.jrkabao.widget;

import android.app.Activity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.mifengkong.frtools.ui.adapter.recyclerview.FRBaseRecyclerAdapter;
import com.mifengkong.frtools.inputmethod.FRView;
import com.mifengkong.frwidget.popup_window.base.SlideFromBottomBasePopupMenu;

import java.util.List;

import cn.rongdao.jrkabao.R;
import cn.rongdao.jrkabao.bean.cashadvance.CashByCreditCardBean;
import cn.rongdao.jrkabao.ui.cashadvance.adapter.DialogSelectFeeAdapter;
import cn.rongdao.jrkabao.ui.cashadvance.adapter.DialogSelectFeeViewHolder;
import cn.rongdao.jrkabao.ui.cashadvance.uicomponent.DialogFromBottomHeader;

/**
 * Created by jack on 2017/9/12
 */

public class SelectFeeFromBottom implements FRBaseRecyclerAdapter.OnItemClickListener<DialogSelectFeeViewHolder> {

    private SlideFromBottomBasePopupMenu mSlideFromBottomBasePopupMenu;

    private DialogSelectFeeAdapter selectFeeAdapter;

    private OnDialogSelectCardClickListener onDialogSelectCardClickListener;

    private String money;

    private int selectPosition = 0;

    public SelectFeeFromBottom(final Activity activity) {
        View itemView = FRView.inflateLayout(activity, R.layout.dialog_setect_credit_card);
        DialogFromBottomHeader dialogFromBottomHeader = itemView.findViewById(R.id.dscc_select_card);
        RecyclerView recyclerView = itemView.findViewById(R.id.dscc_recyclerview);

        mSlideFromBottomBasePopupMenu = new SlideFromBottomBasePopupMenu(activity);
        mSlideFromBottomBasePopupMenu.setMenuItems(itemView);

        selectFeeAdapter = new DialogSelectFeeAdapter(activity, this);
        recyclerView.setLayoutManager(new LinearLayoutManager(activity));
        recyclerView.setAdapter(selectFeeAdapter);

        dialogFromBottomHeader.setNameAndOnClickListener("选择费率", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    public void setAdapterDataList(List<CashByCreditCardBean.PassWay> list, String id, String money) {
        if (null != selectFeeAdapter) {
            this.money = money;
            selectFeeAdapter.setDataList(list);
            if (list.size() > 0) {
                selectPosition = getListIndex(id);
                selectFeeAdapter.setSelectFee(selectPosition, money);
            }
        }
    }

    private int getListIndex(String id) {
        for (int i = 0; i < selectFeeAdapter.getDataList().size(); i++) {
            if (id.equals(selectFeeAdapter.getDataList().get(i).getId())) {
                return i;
            }
        }
        return -1;
    }

    public void dismiss() {
        if (null != mSlideFromBottomBasePopupMenu) {
            mSlideFromBottomBasePopupMenu.dismiss();
        }
    }

    public void show() {
        if (null != mSlideFromBottomBasePopupMenu) {
            mSlideFromBottomBasePopupMenu.showPopupWindow();
        }
    }

    public interface OnDialogSelectCardClickListener {
        void onItemClick(int position);
    }

    public void setOnDialogSelectCardClickListener(OnDialogSelectCardClickListener onDialogSelectCardClickListener) {
        this.onDialogSelectCardClickListener = onDialogSelectCardClickListener;
    }

    @Override
    public void onItemClick(DialogSelectFeeViewHolder dialogSelectFeeViewHolder, int position) {
        if (position == selectPosition) {
            dismiss();
            return;
        }
        selectPosition = position;
        selectFeeAdapter.setSelectFee(selectPosition, money);
        if (null != onDialogSelectCardClickListener) {
            onDialogSelectCardClickListener.onItemClick(position);
        }
    }
}